package com.bls.blslib.utils;

import android.app.Activity;
import android.graphics.Rect;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewTreeObserver;

/**
 * https://www.zhihu.com/question/44269936
 * <p>Detects Keyboard Status changes and fires events only once for each change.</p>
 * <p>
 * 软键盘显示隐藏监听事件
 */
public class KeyboardStatusDetector {
    private static final int SOFT_KEY_BOARD_MIN_HEIGHT = 100;
    private KeyboardVisibilityListener mVisibilityListener;

    boolean keyboardVisible = false;

    public KeyboardStatusDetector registerFragment(Fragment f) {
        registerView(f.getView());
        return this;
    }

    public KeyboardStatusDetector registerActivity(Activity a) {
        registerView(a.getWindow().getDecorView().findViewById(android.R.id.content));
        return this;
    }

    public KeyboardStatusDetector registerView(final View v) {
        v.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                v.getWindowVisibleDisplayFrame(r);

                int heightDiff = v.getRootView().getHeight() - (r.bottom - r.top);
                if (heightDiff > SOFT_KEY_BOARD_MIN_HEIGHT) { // if more than 100 pixels, its probably a keyboard...
                    if (!keyboardVisible) {
                        keyboardVisible = true;
                        if (mVisibilityListener != null) {
                            mVisibilityListener.onKeyboardVisibilityChanged(true);
                        }
                    }
                } else {
                    if (keyboardVisible) {
                        keyboardVisible = false;
                        if (mVisibilityListener != null) {
                            mVisibilityListener.onKeyboardVisibilityChanged(false);
                        }
                    }
                }
            }
        });
        return this;
    }

    public KeyboardStatusDetector setKeyBoardVisibilityListener(KeyboardVisibilityListener listener) {
        mVisibilityListener = listener;
        return this;
    }

    public interface KeyboardVisibilityListener {
        void onKeyboardVisibilityChanged(boolean keyboardVisible);
    }

/*
  使用：

  1.在支持分屏的Android系统下，这个策略可能有问题。

    2.如果在Fragment中使用，记得退出时remove掉ViewTreeObserver的监听，否则会造成view的泄露。

  new KeyboardStatusDetector()
                .registerFragment(fragment)  //register to a fragment
                .registerActivity(activity)  //or register to an activity
                .registerView(view)          //or register to a view
                .setVisibilityListener(new KeyboardVisibilityListener() {
        @Override
        public void onKeyboardVisibilityChanged(boolean keyboardVisible) {
            if (keyboardVisible) {
                //Do stuff for keyboard visible
            } else {
                //Do stuff for keyboard hidden
            }
        }
    });*/

}